package com.edyd.customer.api.context;


import com.edyd.customer.api.domain.Client;
import com.edyd.customer.api.domain.Customer;
import com.edyd.customer.api.service.CustomerService;
import com.fow.core.platform.http.HTTPHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

;

/**
 * Created by Greg.Chen on 2015/1/19.
 */
@Component
public class ActionContext {
    public static final String CURRENT_CLIENT = "currentClient";
    public static final String CURRENT_CUSTOMER = "currentCustomer";


    @Autowired(required = false)
    public HttpServletRequest request;

    @Autowired
    private CustomerService customerService;

    public Client getCurrentClient(){
        return (Client) request.getAttribute(CURRENT_CLIENT);
    }

    public int getCustomerId(){

        return (int) request.getAttribute(HTTPHeaders.HEADER_CUSTOMER_ID);
    }

    public Customer getCustomer(){

        Object customer =  customerService.getFromCache(getCustomerId());
        return (Customer) customer;
    }
}
